
package com.be.beadmin.material.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.material.dto.input.PickingOverViewDto;
import com.be.beadmin.material.dto.output.PickingOverViewEx;
import com.be.beadmin.material.entity.PickingOverView;
import com.be.beadmin.material.entity.PickingOverView_;
import com.be.beadmin.material.mapper.PickingOverViewMapper;
import com.be.beadmin.material.mapper.PickingOverViewMapperEx;
import com.be.beadmin.material.repository.PickingOverViewRepository;
import com.be.beadmin.material.service.PickingOverViewService;
import com.be.beadmin.product.entity.WorkOrderInfor;
import com.be.beadmin.service.service.SerialNumberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zmy
* @date 2023-05-14
**/
@Service
@RequiredArgsConstructor
public class PickingOverViewServiceImpl implements PickingOverViewService {

    @Autowired
    private PickingOverViewRepository pickingOverViewRepository;
    @Autowired
    private PickingOverViewMapper pickingOverViewMapper;
    @Autowired
    private PickingOverViewMapperEx pickingOverViewExMapper;
    @Autowired
    private SerialNumberService serialNumberService;
    @Override
    public Page<PickingOverViewEx> getPickingOverViewByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<PickingOverView> page = pickingOverViewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PickingOverView_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PickingOverView_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<PickingOverViewEx> page1 = page.map(pickingOverViewExMapper::toEntity);
            return page1;

    }

    @Override
    public void addPickingOverView(PickingOverViewDto pickingOverViewDto) {
            PickingOverView result = pickingOverViewRepository.findPickingOverViewByIdAndDelete(pickingOverViewDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("PickingOverView不存在,请重新查询");
            }
            PickingOverView pickingOverView = pickingOverViewMapper.toEntity(pickingOverViewDto);
            pickingOverViewRepository.save(pickingOverView);
    }

    @Override
    public void updatePickingOverView(PickingOverViewDto pickingOverViewDto) {
            PickingOverView pickingOverView = pickingOverViewRepository.findPickingOverViewById(pickingOverViewDto.getId());
            pickingOverView = pickingOverViewMapper.toEntity(pickingOverViewDto);
            pickingOverViewRepository.save(pickingOverView);
    }


    @Override
    public void delPickingOverView(String id) {
        PickingOverView pickingOverView = pickingOverViewRepository.findPickingOverViewById(id);
        pickingOverView.setDelete(1);
        pickingOverViewRepository.save(pickingOverView);
    }

    @Override
    public void delPickingOverViews (List<String> ids) { }

    /**
     * 下发分料任务
     *
     * @param workOrderInfor
     */
    @Override
    public void deliverPickingTask(WorkOrderInfor workOrderInfor) {
        PickingOverView pickingOverView = new PickingOverView();
        pickingOverView.setProductCode(workOrderInfor.getBatchProductCode());
        pickingOverView.setPickingNo(serialNumberService.getNextSnByCode("PickingNo"));
        pickingOverView.setPickingState("未分料");
        pickingOverView.setRecieveStatus("待领料");
        pickingOverViewRepository.save(pickingOverView);
    }
}